/**
 * Created by xuxin on 2017/3/3.
 */
import Vue from 'vue'
let AlertConstructor = Vue.extend(require('../../components/popbox/alert.vue'))

let instance

let Alert = function (options) {
  this.$Bus.$on(['alert', 'pop'], function () {
    instance.visible = false
  })
  let self = this
  options = options || {}
  if (typeof options === 'string') {
    options = {
      msg: options
    }
  }

  let confirm = options.onConfirm

  if (confirm) {
    options.onConfirm = function () {
      instance.visible = false
      confirm.call(self)
    }
  }

  let onMask = options.onMask

  if (onMask) {
    options.onMask = function () {
      instance.visible = false
      onMask.call(self)
    }
  }
  if (!options.confirmText) {
    options.confirmText = '确定'
  }

  if (!instance) {
    instance = new AlertConstructor({
      data: options
    })
    // 文档外渲染生成$el
    instance.$mount()
    document.body.appendChild(instance.$el)
    instance.dom = instance.$el
  } else {
    Object.keys(options).forEach(function (key) {
      if (instance[key]) {
        instance[key] = options[key]
      }
    })
  }
  instance.visible = true
  return instance
}

export default Alert
